代理程式範本 - 處理選擇文件($Agent Template - Process Selected)
今天要介紹的是老少咸宜的東西,把這存起來對之後新舊手來說都是一個好的片段程式碼,做這項功能時就不用再去哪找一些範例還改了,直接用就上手.
常常會用到的就是要處理多份文件,
這一個代理程式範本就可以直接拿來套用來改,還可以計算執行百分比..
Option Public
Option Declare
'Use "OpenLogFunctions"
Sub Initialize
On Error Goto Errorhandler
Dim ws As New NotesUIWorkspace
Dim session As New NotesSession
Dim db As NotesDatabase
Dim dc As NotesDocumentCollection
Dim doc As NotesDocument
Dim total As Long
Dim docnum As Long
'Get a handle on the selected documents
Set db = session.CurrentDatabase
'Optional OpenLog command to force logging to the current database - remove to use default (which may also be current db)
'Call UseCustomLogDb (db)
Set dc = db.UnprocessedDocuments
total = dc.Count
docnum = 0
Set doc = dc.GetFirstDocument
While Not doc Is Nothing
docnum = docnum + 1
Call UpdateStatusBar(docnum, total)
Call ProcessDocSample(doc)
'<<<<<<<<< Insert your code here >>>>>>>>>>>
doc.Form = "Test"
'Call doc.ComputeWithForm(False, False) 'unrem if you want all form fields to recompute
Call doc.Save(True, False)
Set doc = dc.GetNextDocument(doc)
Wend
Call ws.ViewRefresh
Exit Sub
Errorhandler:
'See the "Using This Database" for details on error handling with OpenNTF OpenLog
'Call LogError
Exit Sub
End Sub
Sub ProcessDocSample(doc As NotesDocument)
On Error Goto stackError
'<<<<<<<< Do stuff to the document >>>>>>>>>
Exit Sub
stackError:
'This will report any errors here up to the calling sub/function (e.g. "Initialize")
'Call AddToStackTrace()
End Sub
Sub UpdateStatusBar(x As Long, total As Long)
'This will display the percentage complete in the status bar as the agent runs
Print "Working..." & Cstr(Round((x / total), 2)*100) & "% done"
End Sub